package com.zt.rongda.ad.controller;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zt.rongda.ad.model.AdDesign;
import com.zt.rongda.ad.service.AdDesignService;
import com.zt.servlet.ReqBean;
import com.zt.servlet.RequestAttribute;
import com.zt.servlet.Response;
import com.zt.util.StringUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping(value = "/adDesign")
public class AdDesignController {

    @Autowired
    private AdDesignService adDesignService;
  
    @RequestMapping(value = "/query")
    @ResponseBody
    public JSONObject query(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(adDesignService.getTable(reqBean));
    }
    
    @RequestMapping(value = "/add")
    @ResponseBody
    public JSONObject add(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        adDesignService.add(getAdDesign(reqBean), getAdDesignSamples(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/update")
    @ResponseBody
    public JSONObject update(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        adDesignService.update(getAdDesign(reqBean), getAdDesignSamples(reqBean));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/delete")
    @ResponseBody
    public JSONObject delete(@RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        adDesignService.delete(reqBean.getInfo("adId"));
        return Response.responseSuccess();
    }

    @RequestMapping(value = "/downloadFile")
    @ResponseBody
    public JSONObject downloadFile(final @RequestAttribute(ReqBean.NAME) ReqBean reqBean, HttpServletRequest request,
                                   HttpServletResponse response) throws Exception {
        adDesignService.downloadFile(reqBean, request, response);
        return Response.responseSuccess();
    }
    
    @RequestMapping(value = "/list/{customerId}")
    @ResponseBody
    public JSONObject getCustomerAds(@PathVariable String customerId, @RequestAttribute(ReqBean.NAME) ReqBean reqBean) {
        return Response.response(adDesignService.getCustomerAds(customerId, reqBean));
    }

    /**
    * 从表单获取对象
    *
    * @param reqBean
    * @return
    */
    private AdDesign getAdDesign(ReqBean reqBean) {
        AdDesign adDesign = new AdDesign();
        adDesign.setAdId(reqBean.getInfo("adId"));
        adDesign.setAdName(reqBean.getInfo("adName"));
        adDesign.setCustomerId(reqBean.getInfo("customerId"));
        adDesign.setSampleData(reqBean.getInfo("sampleData"));
        adDesign.setPath(reqBean.getInfo("path"));
        adDesign.setSampleSource(reqBean.getInfo("sampleSource"));
        adDesign.setMachineNo(reqBean.getInfo("machineNo"));
        adDesign.setPrintPrecision(reqBean.getInfo("printPrecision"));
        adDesign.setStatus(reqBean.getInfo("status"));
        adDesign.setOperator(reqBean.getUserId());
        adDesign.setRemark(reqBean.getInfo("remark"));
        return adDesign;
    }
    
    /**
     * 从表单获取文件列表
     *
     * @param reqBean
     * @return
     */
     private JSONArray getAdDesignSamples(ReqBean reqBean) {
         String files = reqBean.getInfo("files");
         if(StringUtil.isNotEmpty(files)){
             return JSONArray.fromObject(files);
         }
         return null;
     }

}